package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.ICartService;
import com.itheima.reggie.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
        @Autowired
        private ICartService cartService;

        @Autowired
        AddressMapper addressMapper;

        @Autowired
        private OrderDetailMapper orderDetailMapper;

        @Autowired
        private OrderMapper orderMapper;

        @Override
        public void saveOrder(Order order) {
            // 1.获取用户信息
            User user = UserHolder.get();
            // 2.获取购物车信息
            List<Cart> cartList = cartService.cartList();
            if (CollectionUtil.isEmpty(cartList)) {
                throw new CustomException("购物车为空不能下单");
            }
            // 3.获取地址信息
            Address address = addressMapper.selectById(order.getAddressId());
            if (address == null) {
                throw new CustomException("收货地址为空不能下单");
            }
            // 4.封装订单明细并保存
            // 4-1 使用雪花算法生成订单id
            long orderId = IdWorker.getId();// mp提供的工具类
            // 4-2 订单总金额
            BigDecimal amount = new BigDecimal(0);
            // 4-3 遍历购物车封装订单明细
            for (Cart cart : cartList) {
                // 创建订单明细
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setName(cart.getName()); // 购物车菜品名称
                orderDetail.setOrderId(orderId);// 订单id
                orderDetail.setDishId(cart.getDishId());// 购物车菜品id
                orderDetail.setSetmealId(cart.getSetmealId());// 购物车套餐id
                orderDetail.setDishFlavor(cart.getDishFlavor());// 购物车口味
                orderDetail.setNumber(cart.getNumber());// 购物车数量
                orderDetail.setAmount(cart.getAmount());// 购物车金额
                orderDetail.setImage(cart.getImage());// 购物车图片

                // 订单总金额累加： amount = amount + (cart.getAmount() * cart.getNumber())
                amount = amount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
                // 保存订单明细
                orderDetailMapper.insert(orderDetail);
            }
            // 5.封装订单并保存
            order.setId(orderId); // 订单id
            order.setNumber(String.valueOf(orderId));// 订单号
            order.setStatus(1); // 1待付款
            order.setUserId(user.getId());// 用户id
            order.setOrderTime(new Date());// 下单时间
            order.setCheckoutTime(new Date()); // 结账时间
            order.setAmount(amount); // 订单金额
            order.setUserName(user.getName()); // 用户名称
            order.setPhone(address.getPhone()); // 收货人手机号
            order.setAddress(address.getDetail()); // 收货人地址
            order.setConsignee(address.getConsignee()); // 收货人名称
            // 保存订单
            orderMapper.insert(order);
            // 6.清空购物车
            cartService.cartClean();
        }

    @Override
    public Page<Order> findOrder(Integer pageNum, Integer pageSize, String number, String beginTime, String endTime) {
        // 1.先查套餐基本信息
        // 1-1 构建条件对象
        LambdaQueryWrapper<Order> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(StrUtil.isNotEmpty(number),Order::getNumber,number);
        if(StrUtil.isNotEmpty(beginTime) && StrUtil.isNotEmpty(endTime)) {
            orderWrapper.between(Order::getOrderTime, beginTime, endTime);
        }
        // 1-2 构建分页对象
        Page<Order> page = new Page<>(pageNum, pageSize);
        // 1-3 查询
        page = orderMapper.selectPage(page, orderWrapper);

//        // 2.获取套餐list集合并遍历
//        List<Order> orderList = page.getRecords();
//        if (CollectionUtil.isNotEmpty(orderList)) {
//            for (Order order : orderList) {
//                // 3.根据category_id查询分类对象
//                User user = userMapper.selectById(order.getUserId());
//
//                order.setUserName(user.getName());
//
//            }
//        }
        // 5.返回结果
        return page;
        }

    @Override
    public Page<Order> orderPage(Integer pageNum, Integer pageSize) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("order_time");


        Page<Order> page = new Page<>(pageNum, pageSize);

        page = orderMapper.selectPage(page,wrapper);

        List<Order> orderList = page.getRecords();
        if (CollectionUtil.isNotEmpty(orderList)) {
            for (Order order : orderList) {
                // 3.查询分类对象
                LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
                orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,order.getId());
                List<OrderDetail> orderDetailList = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
                order.setOrderDetails(orderDetailList);
            }
        }


        return page;
    }


}
